
package javaapplication64;

import java.util.logging.Level;
import java.util.logging.Logger;

class ThreadTest extends Thread{
    private Test test;
    public ThreadTest(String name,Test test){
        super(name);
        this.test=test;
    }
    public void run(){
        test.method();
    }
}


public class Test {
    public static void main(String[] args) {
        Test test1 =new Test();
        Test test2=new Test();
        ThreadTest thread1=new ThreadTest("线程1", test1);
        ThreadTest thread2=new ThreadTest("线程2", test1);
        thread1.start();
        thread2.start();
    }

        public synchronized void method(){
            System.out.println(Thread.currentThread().getName()+"获得锁");
            try {
                System.out.println(Thread.currentThread().getName()+"开始方法");
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName()+"结束方法");
            } catch (Exception ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE,null,ex);
            }

}
    }   
    

